// pages/safe/safe.js
let app = getApp()
const db = wx.cloud.database();
const userCollection = db.collection("user");
let _ = db.command;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null, //个人信息
        show: false, //弹框
        oldpassword: null, //旧密码
        newpassword: null, //新密码
    },
    onShow() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/findAccountInfo',
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token
            },
            success: res => {
                console.log(res);
                this.setData({
                    userInfo: res.data.result[0]
                })
            }
        })
    },
    // 导航栏事件
    // onClickLeft() {
    //     wx.showToast({
    //         title: '点击返回',
    //         icon: 'none'
    //     });
    // },
    // 退出登录
    edit() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/logout',
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            },
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token
            },
            success: res => {
                console.log(res);
                if (res.data.msg == "退出登录成功") {
                    wx.removeStorage({
                        key: 'token',
                    })
                    app.globalData.token = ""
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
            }
        })
    },
    // 修改密码
    changePassword() {
        if (!this.data.oldpassword || !this.data.newpassword) {
            wx.showToast({
                title: '密码不能为空',
                icon: 'error'
            })
        } else {
            // 先查找当前密码是否正确
            userCollection.where({
                password: this.data.oldpassword
            }).get().then(re => {
                console.log(re);
                if (re.data.length <= 0) {
                    wx.showToast({
                        title: '旧密码不正确',
                        icon: 'error'
                    })
                } else {
                    wx.request({
                        url: 'http://www.kangliuyong.com:10002/updatePassword',
                        method: 'POST',
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                        },
                        data: {
                            appkey: app.globalData.appkey,
                            tokenString: app.globalData.token,
                            password: this.data.newpassword,
                            oldPassword: this.data.oldpassword
                        },
                        success: res => {
                            console.log(res);
                            if (res.data.msg == "修改密码成功") {
                                userCollection.where({
                                    phone: this.data.userInfo.phone
                                }).update({
                                    data: {
                                        password: this.data.newpassword
                                    }
                                }).then(re => {
                                    console.log("修改成功", re)
                                }).catch(err => {
                                    console.log("修改失败", err);
                                })
                                wx.showModal({
                                    title: res.data.msg,
                                    content: '请重新登录',
                                    success: (res) => {
                                        if (res.confirm) {
                                            console.log('用户登录');
                                            wx.navigateTo({
                                                url: '/pages/login/login',
                                            })
                                        } else if (res.cancel) {
                                            console.log('用户取消');
                                            wx.navigateTo({
                                                url: '/pages/login/login',
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    })
                }
            })
        }
    },
    // 注销账号
    cancelAccount() {
        wx.showModal({
            title: '注销账号',
            content: '注销后无法恢复',
            success: (res) => {
                if (res.confirm) {
                    console.log('用户确定');
                    wx.request({
                        url: 'http://www.kangliuyong.com:10002/destroyAccount',
                        method: 'POST',
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                        },
                        data: {
                            appkey: app.globalData.appkey,
                            tokenString: app.globalData.token,
                        },
                        success: res => {
                            console.log(res);
                            if (res.data.msg == "注销账号成功") {
                                wx.removeStorage({
                                    key: 'token',
                                })
                                app.globalData.token = ""
                                const db = wx.cloud.database();
                                const userCollection = db.collection("user");
                                let _ = db.command;
                                userCollection.where({
                                    phone: this.data.userInfo.phone,
                                }).remove().then(re => {
                                    console.log("删除成功", re)
                                }).catch(err => {
                                    console.log("删除失败", err)
                                })
                                wx.switchTab({
                                    url: '/pages/index/index',
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户取消');
                }
            }
        })

    },
    // 弹框事件
    showPopup() {
        this.setData({
            show: true
        });
    },
    onClose() {
        this.setData({
            show: false
        });
    },
})